package sample.androidwidget;

import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.os.Bundle;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.util.List;

import sample.androidwidget.view.GestureOverlay;

public class GestureOverlayViewActivity extends Activity {

    private static final String TAG = "GestureOverlayViewActivity";

    private GestureLibrary gestureLibrary;
    private Button record, recognize;
    private GestureOverlay gesture;
    public final static String GESTURE = "gesture";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gesture_overlay_view);

        gestureLibrary = GestureLibraries.fromFile(new File(getCacheDir(), "gesture.dat"));

        record = (Button) findViewById(R.id.record);
        recognize = (Button) findViewById(R.id.recognize);
        gesture = (GestureOverlay) findViewById(R.id.gesture);
        record.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (recognize.isSelected()) {
                    return;
                }
                gesture.setSelected(!gesture.isSelected());
                if (gesture.isSelected()) {
                    gestureLibrary.removeEntry(GESTURE);
                    record.setText("录制中");
                } else {
                    if (gestureLibrary.getGestures(GESTURE) == null) {
                        Toast.makeText(GestureOverlayViewActivity.this, "请输入手势", Toast.LENGTH_SHORT).show();
                    }
                    record.setText("录制手势");
                }
            }
        });
        recognize.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (gesture.isSelected()) {
                    return;
                }
                if (gestureLibrary.getGestures(GESTURE) == null) {
                    Toast.makeText(GestureOverlayViewActivity.this, "请输入手势", Toast.LENGTH_SHORT).show();
                    return;
                }
                recognize.setSelected(!recognize.isSelected());
                if (recognize.isSelected()) {
                    recognize.setText("识别中");
                } else {
                    recognize.setText("识别手势");
                }
            }
        });
        findViewById(R.id.savegesture).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (gestureLibrary.getGestures(GESTURE) == null) {
                    Toast.makeText(GestureOverlayViewActivity.this, "请输入手势", Toast.LENGTH_SHORT).show();
                    return;
                }

            }
        });

        gesture.addOnGesturingListener(new GestureOverlayView.OnGesturingListener() {
            @Override
            public void onGesturingStarted(GestureOverlayView overlay) {
            }

            @Override
            public void onGesturingEnded(GestureOverlayView overlay) {
                if (!gesture.isUsefulGesture()) {
                    Toast.makeText(GestureOverlayViewActivity.this, "无效手势", Toast.LENGTH_SHORT).show();
                }
            }
        });
        gesture.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
            @Override
            public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
                Log.d("PerformedListener", "onGesturePerformed");
                if (recognize.isSelected()) {
                    recognize.setSelected(false);
                    recognize.setText("识别手势");

                    List<Prediction> list = gestureLibrary.recognize(gesture);//识别手势，返回一个类型为Prediction的列表
                    if (!list.isEmpty()) {
                        Prediction pre = list.get(0);
                        if (pre.score > 2) { //如果匹配度大于2，表示可以识别，否则提示无法识别
                            if (pre.name.equals(GESTURE)) {//判断名字是否与手势库的名字相同
                                Toast.makeText(GestureOverlayViewActivity.this, "识别成功", Toast.LENGTH_SHORT).show();
                            }
                            return;
                        }
                    }
                    Toast.makeText(GestureOverlayViewActivity.this, "无法识别", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (overlay.isSelected()) {
                    overlay.setSelected(false);
                    record.setText("录制手势");
                    gestureLibrary.addGesture(GESTURE, gesture);
                }
            }
        });

        Window window = getWindow();
        View dectorView = window.getDecorView();
        Window container = window.getContainer();

        View parent = (View) dectorView.getParent();
        View rootView = dectorView.getRootView();


        rootView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
            @Override
            public void onViewAttachedToWindow(View v) {

            }

            @Override
            public void onViewDetachedFromWindow(View v) {

            }
        });
    }

    //分别从上到下遍历view信息
    public static void printViews(View someView) {
        View parent = (View) someView.getParent();
        if (parent != null) {
            
        }
    }

}
